package
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	
	public class isotest extends Sprite
	{
		private var world:World;
		
		public function isotest()
		{
			stage.frameRate = 60;
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		
			//Globals.rendererFactory = new RendererFactory(RendererFactory.RENDER_TYPE_NATIVE);
			Globals.rendererFactory = new RendererFactory(RendererFactory.RENDER_TYPE_ND2D);
			Globals.lastTime = Globals.currentTime = getTimer();	
			Globals.stage = stage;
			Globals.transformer = new IsometricTransformer();
			
			ImageManager.startLoad(function():void{
				go();
			});
			
			
		}
		
		private function go():void{
			var i:int, il:int, j:int, jl:int, k:int, kl:int;
			world = new World();
			var profile:TextField = new TextField();
			var cfg:WorldConfig = new WorldConfig();

			cfg.renderStyle = WorldConfig.RENDER_STYLE_ISOMETRIC;
			//cfg.renderStyle = WorldConfig.RENDER_STYLE_SIMPLE_GRID;
			
			world.configure(cfg);
			world.setParent(this);
			
			profile.textColor = 0xff0000;
			profile.width = 100;
			addChild(profile);
			addEventListener(Event.ENTER_FRAME, function(e:Event):void{
				Globals.currentTime = getTimer();
				Globals.deltaTime = Globals.currentTime - Globals.lastTime;
				profile.text = String( Math.floor(1000 / Globals.deltaTime) ) + ' mem: ' + String(Math.floor(System.totalMemory/1000000)) + "MB";
				
				Globals.lastTime = Globals.currentTime;
				world.update();
				update();
			});
			
			stage.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{
				for(var n:int = 0; n < 50; n++){
					var gobj:GameObject = new GameObject();
					gobj.setPosition(new Vector3D(Math.floor(Math.random()*Globals.WORLD_SIZE),Math.floor(Math.random()*Globals.WORLD_SIZE),0));
					//gobj.setPosition(new Vector3D(0,0,0));
					world.add(gobj);
					addToSimulation(gobj);
					gobj.depthSort();
				}
			});
			
			var hero:GameObject = new GameObject();
			hero.setPosition(new Vector3D(0,0,0));
			world.add(hero);
			
			var onMouseMove:Function = function(me:MouseEvent):void{
				var pos:Vector3D = new Vector3D((me.currentTarget as Stage).mouseX, (me.currentTarget as Stage).mouseY,0);
				var offset:Vector3D = new Vector3D(-Globals.stage.stageWidth/2,-Globals.stage.stageHeight/2 - Globals.WORLD_SIZE*10, 0);
				pos = pos.add(offset);
				var rev:Vector3D = Globals.transformer.reverseTransform(pos);
				hero.setPosition(rev);
				hero.depthSort();
			}
				
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			
//			stage.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{
//				addToSimulation(hero);
//				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
//			});

		}
		
		
		public function update():void{
			var i:int, il:int;
			var gobj:GameObject;
			
			for(i = 0, il = m_gameObjects.length; i < il; i++){
				gobj = m_gameObjects[i];
				gobj.update();
			}
		}		
		
		private var m_gameObjects:Vector.<GameObject> = new Vector.<GameObject>;
		public function addToSimulation(gobj:GameObject):void{
			m_gameObjects.push(gobj);
		}
	}
}